<?php

namespace app\admin\command;

use addons\shopro\service\commission\Agent as AgentService;
use app\admin\model\shopro\commission\Agent;
use app\admin\model\shopro\user\WalletLog;
use app\admin\model\User;
use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use app\common\model\TokensLog;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class UserMove extends Command
{
    protected function configure()
    {
        $this->setName("user-move");
    }

    protected function execute(Input $input, Output $output)
    {

        Db::name('user')
            ->chunk(20, function ($list) {
                foreach ($list as $item) {
                    $agent = new AgentService($item['id']);
                    if ($agent->user) {
                        Db::transaction(function () use ($agent) {
                            $agent->runAgentUpgradePlan();
                        });

                    }
                }
            });


//        Db::name('user')
//            ->where('old_uid', '>', 0)
//            ->chunk(20, function ($list) {
//                foreach ($list as $item) {
//                    $oldUser = Db::name('user_copy2')
//                        ->where('id', $item['old_uid'])
//                        ->find();
//                    $data = [
//                        'money' => $oldUser['money'],
//                        'commission' => $oldUser['commission'],
//                        'total_score' => $oldUser['total_score'],
//                        'score' => $oldUser['score'],
//                        'tokens' => $oldUser['tokens'],
//                        'successions' => $oldUser['successions'],
//                        'maxsuccessions' => $oldUser['maxsuccessions'],
//                        'total_consume' => $oldUser['total_consume'],
//                    ];
//                    Db::name('user')
//                        ->where('id', $item['id'])
//                        ->update($data);
//
//                    $oldCommission = Db::name('shopro_commission_agent_copy2')
//                        ->where('user_id', $item['old_uid'])
//                        ->find();
//
//                    $param = [
//                        'level' => $oldCommission['level'],
//                        'tokens_level' => $oldCommission['tokens_level'],
//                        'apply_info' => $oldCommission['apply_info'],
//                        'total_income' => $oldCommission['total_income'],
//                        'child_order_money_0' => $oldCommission['child_order_money_0'],
//                        'child_order_money_1' => $oldCommission['child_order_money_1'],
//                        'child_order_money_2' => $oldCommission['child_order_money_2'],
//                        'child_order_money_all' => $oldCommission['child_order_money_all'],
//                        'child_order_count_0' => $oldCommission['child_order_count_0'],
//                        'child_order_count_1' => $oldCommission['child_order_count_1'],
//                        'child_order_count_2' => $oldCommission['child_order_count_2'],
//                        'child_order_count_all' => $oldCommission['child_order_count_all'],
//                        'child_agent_count_1' => $oldCommission['child_agent_count_1'],
//                        'child_agent_count_2' => $oldCommission['child_agent_count_2'],
//                        'child_agent_count_all' => $oldCommission['child_agent_count_all'],
//                        'child_agent_level_1' => $oldCommission['child_agent_level_1'],
//                        'child_agent_level_all' => $oldCommission['child_agent_level_all'],
//                        'child_user_count_1' => $oldCommission['child_user_count_1'],
//                        'child_user_count_2' => $oldCommission['child_user_count_2'],
//                        'child_user_count_all' => $oldCommission['child_user_count_all'],
//                        'upgrade_lock' => $oldCommission['upgrade_lock'],
//                        'apply_num' => $oldCommission['apply_num'],
//                        'level_status' => $oldCommission['level_status'],
//                        'status' => $oldCommission['status'],
//                        'become_time' => $oldCommission['become_time'],
//                        'createtime' => $oldCommission['createtime'],
//                        'updatetime' => $oldCommission['updatetime'],
//                        'today_order_count' => $oldCommission['today_order_count'],
//                        'yesday_order_count' => $oldCommission['yesday_order_count'],
//                        'today_commission_mony' => $oldCommission['today_commission_mony'],
//                        'yesday_commission_mony' => $oldCommission['yesday_commission_mony'],
//                        'today_order_money' => $oldCommission['today_order_money'],
//                        'yesday_order_money' => $oldCommission['yesday_order_money'],
//                        'today_team_money' => $oldCommission['today_team_money'],
//                        'yesday_team_money' => $oldCommission['yesday_team_money'],
//                        'today_driect_money' => $oldCommission['today_driect_money'],
//                        'yesday_driect_money' => $oldCommission['yesday_driect_money'],
//                        'score_total_all' => $oldCommission['score_total_all'],
//                    ];
//
//                    Db::name('shopro_commission_agent')
//                        ->where('user_id', $item['id'])
//                        ->update($param);
//                }
//            });
    }


    public function loopUser($userIds, &$count)
    {
        foreach ($userIds as $id) {
            $user = Db::name('user')
                ->where('old_uid', $id)
                ->find();
            Db::name('user_tokens_log2')
                ->where('user_id', $id)
                ->chunk(20, function ($list) use (&$count, $user) {
                    foreach ($list as $item) {
                        $param = [
                            'user_id'  => $user['id'],
                            'tokens'=> $item['tokens'],
                            'before'=> $item['before'],
                            'after'=> $item['after'],
                            'memo'=> $item['memo'],
                            'createtime'=> $item['createtime'],
                        ];
                        TokensLog::create($param);
                        $count++;
                    }
                });
        }
    }
}
